﻿/*
*  CustomerManager.js
*  zhouh@zd_20160404   
*/
$(function () {
    //announcement
    var divUser = new jSharp.popUpDiv('divUser');
    var divMsg = new jSharp.popUpDiv('divMsg');
    var sellerList = $('#ddlSeller');
    var currencyList = $("[id$='ddlCurrency']");
    var txtCusRank = $('#txtCusRank');
    var txtCusRate = $('#txtCusRate');
    var txtUid = $('#txtUid');

    //function
    function loadUserInfo(uid) {//加载客户信息 
        $.get("../Common/AjaxHandler.aspx", {
            handler: "getCus",
            uid: uid
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                txtCusRank.val(o[0]["tauAdditional6"]);
                txtCusRate.val(o[0]["tauAdditional4"]);
                var cl = currencyList.children();
                for (var i = 0; i < cl.length; i++) {
                    $(cl[i]).removeAttr("selected");
                    if ($(cl[i]).attr("value") == o[0]["tauAdditional5"]) {
                        $(cl[i]).attr("selected", "selected");
                    }
                }
                if (o[0]["belongto"] == "无") {
                    $('#trSell').show();
                }
                else {
                    $('#trSell').hide();
                }
            }
        })
    }
    function updateSellerList(name) {//根据输入的过滤条件加载销售列表
        sellerList.empty();
        $.get("../Common/AjaxHandler.aspx", {
            handler: "getSeller",
            name: name
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                var s = '<option value="">无</option>';
                for (var i = 0; i < o.length; i++) {
                    s += '<option value="' + o[i]["tauAdditional1"] + '">' + o[i]["tauName"] + '</option>';
                }
                sellerList.html(s);
            }
        })
    }
    function save() {//保存
        var rank = txtCusRank.val();
        var rate = txtCusRate.val();
        var currency = currencyList.find("option:selected").attr("value");
        var seller = $('#ddlSeller').find("option:selected").attr("value");
        console.log(seller);
        var uid = txtUid.val();
        if (rank == "") {
            tips("请填写用户等级！");
            return;
        }
        if (rate == "") {
            tips("请填写用户系数！");
            return;
        }
        $.post("../Common/AjaxHandler.aspx", {
            handler: "editCus",
            rank: rank,
            rate: rate,
            belongTo: seller,
            currency: currency,
            uid: uid
        }, function (data) {
            console.log(data);
            if (data == "OK") {
                tips("修改成功！");
                location.reload();
            }
            if (data == "out of range") {
                tips("客户系数超过允许范围");
            }
        })
    }
    function tips(s) {
        divMsg.Show();
        $('#msgContent').html(s);
    }
    function filterString(s, maxLen) {//Xss反注入
        if (s.length > maxLen) return false;
        s = s.replace(/ /g, "");
        var param = new Array("<", "%3c", ">", "%3e", "script", "document", "eval", "/", "%2f", "//", "\\(", "\\)");
        for (var i = 0; i < param.length; i++) {
            var reg = new RegExp(param[i]);
            if (reg.test(s)) return false;
        }
        return true;
    }
    //handler
    $('#hideUser').click(function () {
        divUser.Close();
    })
    $('#hideMsg').click(function () {
        divMsg.Close();
    })
    $('#btnSave').click(function () {
        save();
    })
    $('.btnUpdate').each(function (index, elt) {
        $(elt).click(function () {
            divUser.Show();
            var uid = $(elt).attr('args');
            txtUid.val(uid);
            updateSellerList("");
            loadUserInfo(uid);
        })
    })
    $('#txtFilter').keyup(function () {
        var name = $('#txtFilter').val();
        if (name != "") {
            name = name.replace(/ /g, "");
        }
        updateSellerList(name);
    })
    //init
} ())